home *** CD-ROM | disk | FTP | other *** search
- // Open via Map Extension.c
-
- #include "Open via Map.h"
-
- #include <MoreDesktopMgr.h>
-
- #include <InternetConfig.h>
-
- Boolean gQuit;
- Boolean gIsActive;
- ICInstance gIConfig;
-
- static void Initialize(void)
- {
- InitGraf(&qd.thePort);
- MaxApplZone();
- }
-
- static OSErr DoQuit(void)
- {
- gQuit = true;
-
- return noErr;
- }
-
- static OSErr GotRequiredParams(const AppleEvent *theEvent)
- {
- OSErr theErr;
- DescType theType;
- Size theSize;
-
- theErr = AEGetAttributePtr(theEvent, keyMissedKeywordAttr, typeWildCard, &theType,
- NULL, 0, &theSize);
-
- if (theErr == errAEDescNotFound)
- theErr = noErr;
- else
- theErr = errAEParamMissed;
-
- return theErr;
- }
-
- static Boolean FindMapping(FSSpec *theSpec, OSType *theOwner)
- {
- Boolean isFound;
- OSStatus theStatus;
- ICMapEntry theEntry;
-
- isFound = false;
-
- theStatus = ICMapFilename(gIConfig, theSpec->name, &theEntry);
-
- if (theStatus == noErr)
- {
- isFound = true;
-
- if (theOwner != NULL)
- *theOwner = theEntry.fileCreator;
- }
-
- return isFound;
- }
-
- static Boolean FindActiveProcessID(OSType theCreator, ProcessSerialNumber *thePSN)
- {
- Boolean isFound;
- ProcessInfoRec theInfo;
- FSSpec theProcessSpec;
- Str31 theName;
-
- isFound = false;
-
- thePSN->highLongOfPSN = 0;
- thePSN->lowLongOfPSN = kNoProcess;
-
- while ((!isFound) && (GetNextProcess(thePSN) == noErr))
- {
- theInfo.processInfoLength = sizeof(theInfo);
- theInfo.processName = (StringPtr) &theName;
- theInfo.processAppSpec = &theProcessSpec;
-
- if (GetProcessInformation(thePSN, &theInfo) == noErr)
- {
- if (theCreator == theInfo.processSignature)
- isFound = true;
- }
- }
-
- if (!isFound)
- {
- thePSN->highLongOfPSN = 0;
- thePSN->lowLongOfPSN = kNoProcess;
- }
-
- return isFound;
- }
-
- static OSErr DoOpenDocument(FSSpec *theSpec)
- {
- OSErr theErr;
- Boolean isFound;
- OSType theCreator;
- ProcessSerialNumber thePSN;
- AEAddressDesc theTarget;
- AppleEvent theOpenEvent;
- AEDesc theAppParms;
- AEDescList theAliases;
- AliasHandle theNewAlias;
- LaunchParamBlockRec pb;
- FSSpec theAppSpec;
-
- FindMapping(theSpec, &theCreator);
-
- isFound = FindActiveProcessID(theCreator, &thePSN);
-
- theErr = noErr;
-
- if (isFound)
- theErr = SetFrontProcess(&thePSN);
-
- if (theErr == noErr)
- theErr = AECreateDesc(typeProcessSerialNumber, &thePSN, sizeof(thePSN),
- &theTarget);
-
- if (theErr == noErr)
- {
- theErr = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &theTarget,
- kAutoGenerateReturnID, kAnyTransactionID, &theOpenEvent);
-
- AEDisposeDesc(&theTarget);
-
- if (theErr == noErr)
- {
- theErr = AECreateList(NULL, 0, false, &theAliases);
-
- if (theErr == noErr)
- {
- theErr = NewAlias(NULL, theSpec, &theNewAlias);
-
- if (theErr == noErr)
- {
- HLock((Handle) theNewAlias);
-
- theErr = AEPutPtr(&theAliases, 0, typeAlias, *theNewAlias,
- GetHandleSize((Handle) theNewAlias));
-
- DisposeHandle((Handle) theNewAlias);
- }
-
- if (theErr == noErr)
- theErr = AEPutParamDesc(&theOpenEvent, keyDirectObject, &theAliases);
-
- AEDisposeDesc(&theAliases);
- }
-
- if (theErr == noErr)
- {
- if (isFound)
- theErr = AESend(&theOpenEvent, NULL, kAENoReply + kAECanInteract,
- kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
- else
- {
- theErr = FSpDTGetAPPL(NULL, 0, theCreator, &theAppSpec);
-
- if (theErr == noErr)
- theErr = AECoerceDesc(&theOpenEvent, typeAppParameters, &theAppParms);
-
- if (theErr == noErr)
- {
- HLock(theAppParms.dataHandle);
-
- pb.launchBlockID = extendedBlock;
- pb.launchEPBLength = extendedBlockLen;
- pb.launchFileFlags = 0;
- pb.launchControlFlags = launchContinue + launchNoFileFlags;
- pb.launchAppSpec = &theAppSpec;
- pb.launchAppParameters = (AppParameters *) *theAppParms.dataHandle;
-
- theErr = LaunchApplication(&pb);
-
- AEDisposeDesc(&theAppParms);
- }
- }
- }
-
- AEDisposeDesc(&theOpenEvent);
- }
- }
-
- return theErr;
- }
-
- static pascal OSErr DoHandleInterceptOpen(const AppleEvent *theEvent, AppleEvent *theReply,
- UInt32 refCon)
- {
- #pragma unused(refCon)
-
- OSErr theErr;
- OSStatus theStatus;
- Boolean areAnySpecial;
- long numItems;
- long theIndex;
- AEDesc theApplication;
- AEDescList theDocuments;
- AEKeyword theKeyword;
- DescType theType;
- Size theSize;
- FSSpec theSpec;
- EventRecord theModEvent;
-
- areAnySpecial = false;
-
- EventAvail(0, &theModEvent);
-
- if (gIsActive && ((theModEvent.modifiers & alphaLock) == 0))
- {
- theErr = AEGetParamDesc(theEvent, 'APPL', typeWildCard, &theApplication);
-
- if (theErr == noErr)
- AEDisposeDesc(&theApplication);
- else if (theErr == errAEDescNotFound)
- {
- theErr = AEGetParamDesc(theEvent, keySelection, typeAEList, &theDocuments);
-
- if (theErr == noErr)
- {
- theErr = GotRequiredParams(theEvent);
-
- if (theErr == noErr)
- theErr = AECountItems(&theDocuments, &numItems);
-
- theStatus = ICBegin(gIConfig, icReadOnlyPerm);
-
- for (theIndex = 1;
- (!areAnySpecial) && (theErr == noErr) && (theIndex <= numItems); theIndex++)
- {
- theErr = AEGetNthPtr(&theDocuments, theIndex, typeFSS, &theKeyword, &theType,
- &theSpec, sizeof(theSpec), &theSize);
-
- if (theErr == noErr)
- areAnySpecial = FindMapping(&theSpec, NULL);
- }
-
- if (areAnySpecial)
- {
- for (theIndex = 1; (theErr == noErr) && (theIndex <= numItems); theIndex++)
- {
- theErr = AEGetNthPtr(&theDocuments, theIndex, typeFSS, &theKeyword, &theType,
- &theSpec, sizeof(theSpec), &theSize);
-
- if (theErr == noErr)
- theErr = DoOpenDocument(&theSpec);
- }
- }
-
- theStatus = ICEnd(gIConfig);
-
- AEDisposeDesc(&theDocuments);
- }
- }
- }
-
- if (theReply != NULL)
- AEPutKeyPtr(theReply, keyAEData, typeBoolean, &areAnySpecial, sizeof(areAnySpecial));
-
- return theErr;
- }
-
- static pascal OSErr DoHandleOpenApplication(const AppleEvent *theEvent,
- AppleEvent *theReply, UInt32 refCon)
- {
- #pragma unused(theReply, refCon)
- OSErr theErr;
-
- theErr = GotRequiredParams(theEvent);
-
- return theErr;
- }
-
- static pascal OSErr DoHandleActiveChange(const AppleEvent *theEvent, AppleEvent *theReply,
- UInt32 refCon)
- {
- #pragma unused(theReply, refCon)
- OSErr theErr;
- DescType theType;
- Size theSize;
- Boolean isActive;
-
- theErr = AEGetParamPtr(theEvent, keyDirectObject, typeBoolean, &theType, &isActive,
- sizeof(isActive), &theSize);
-
- if (theErr == noErr)
- {
- theErr = GotRequiredParams(theEvent);
-
- if (theErr == noErr)
- gIsActive = isActive;
- }
-
- return theErr;
- }
-
- static pascal OSErr DoHandleOpenPrint(const AppleEvent *theEvent, Boolean isOpen)
- {
- #pragma unused(isOpen)
- OSErr theErr;
- AEDescList theDocuments;
-
- theErr = AEGetParamDesc(theEvent, keyDirectObject, typeAEList, &theDocuments);
-
- if (theErr == noErr)
- {
- theErr = GotRequiredParams(theEvent);
-
- AEDisposeDesc(&theDocuments);
- }
-
- return theErr;
- }
-
- static pascal OSErr DoHandleOpenDocuments(const AppleEvent *theEvent, AppleEvent *theReply,
- UInt32 refCon)
- {
- #pragma unused(theReply, refCon)
- OSErr theErr;
-
- theErr = DoHandleOpenPrint(theEvent, true);
-
- return theErr;
- }
-
- static pascal OSErr DoHandlePrintDocuments(const AppleEvent *theEvent, AppleEvent *theReply,
- UInt32 refCon)
- {
- #pragma unused(theReply, refCon)
- OSErr theErr;
-
- theErr = DoHandleOpenPrint(theEvent, false);
-
- return theErr;
- }
-
- static pascal OSErr DoHandleQuit(const AppleEvent *theEvent, AppleEvent *theReply,
- UInt32 refCon)
- {
- #pragma unused(theReply, refCon)
- OSErr theErr;
-
- theErr = GotRequiredParams(theEvent);
-
- if (theErr == noErr)
- theErr = DoQuit();
-
- return theErr;
- }
-
- static void InstallEventHandlers(void)
- {
- OSErr theErr;
-
- theErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
- NewAEEventHandlerProc(&DoHandleOpenApplication), 0, false);
-
- if (theErr == noErr)
- theErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
- NewAEEventHandlerProc(&DoHandleOpenDocuments), 0, false);
-
- if (theErr == noErr)
- theErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
- NewAEEventHandlerProc(&DoHandlePrintDocuments), 0, false);
-
- if (theErr == noErr)
- theErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
- NewAEEventHandlerProc(&DoHandleQuit), 0, false);
-
- if (theErr == noErr)
- theErr = AEInstallEventHandler(kOpenViaMapEventClass, kActiveChange,
- NewAEEventHandlerProc(&DoHandleActiveChange), 0, false);
- }
-
- static void DoHighLevelEvent(EventRecord *theEvent)
- {
- OSErr theErr;
-
- theErr = AEProcessAppleEvent(theEvent);
- }
-
- static void DoEvent(EventRecord *theEvent)
- {
- switch (theEvent->what)
- {
- case kHighLevelEvent:
- DoHighLevelEvent(theEvent);
- break;
- }
- }
-
- static void DoIdle(EventRecord *theEvent)
- {
- #pragma unused(theEvent)
- }
-
- static void MainEventLoop(void)
- {
- Boolean gotEvent;
- UInt32 theSleep;
- EventRecord theEvent;
-
- theSleep = GetCaretTime();
-
- while (!gQuit)
- {
- gotEvent = WaitNextEvent(everyEvent, &theEvent, theSleep, NULL);
-
- if (gotEvent)
- DoEvent(&theEvent);
- else
- DoIdle(&theEvent);
- }
- }
-
- static OSErr GetActive(void)
- {
- OSErr theErr;
- FSSpec theSpec;
- short refNum;
- Preferences **thePreferences;
-
- gIsActive = true;
-
- theErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
- &theSpec.vRefNum, &theSpec.parID);
-
- if (theErr == noErr)
- {
- GetIndString(theSpec.name, kPreferencesStrxID, kPrefNameIndex);
-
- refNum = FSpOpenResFile(&theSpec, fsRdPerm);
- theErr = ResError();
-
- if (theErr == noErr)
- {
- thePreferences = (Preferences **) Get1Resource(kPreferencesType, kPreferencesID);
-
- if (thePreferences && ((**thePreferences).theVersion == kPreferencesVersion))
- gIsActive = (**thePreferences).isActive;
-
- CloseResFile(refNum);
- }
- }
-
- return theErr;
- }
-
- static pascal OSStatus UserSwitching(OSType theMessage, void *theArg, void *theUserRefCon)
- {
- #pragma unused(theArg, theUserRefCon)
-
- if (theMessage == kFolderManagerNotificationMessageUserLogIn)
- GetActive();
- else if (theMessage == kFolderManagerNotificationMessageUserLogOut)
- gIsActive = false;
-
- return noErr;
- }
-
- void main(void)
- {
- OSErr theErr;
- OSStatus theStatus;
- long theResponse;
- AEEventHandlerUPP theHandlerUPP;
- FolderManagerNotificationUPP theNotifyUPP;
-
- gQuit = false;
-
- Initialize();
-
- InstallEventHandlers();
-
- theErr = GetActive();
-
- theHandlerUPP = NewAEEventHandlerUPP(&DoHandleInterceptOpen);
- theErr = AEInstallEventHandler(kAEFinderSuite, kAEInterceptOpen, theHandlerUPP, 0, true);
-
- if (theErr == noErr)
- {
- theErr = Gestalt(gestaltMultipleUsersState, &theResponse);
-
- if ((theErr == noErr) && (**((short **) theResponse) > 0))
- {
- theNotifyUPP = NewFolderManagerNotificationUPP(&UserSwitching);
- theErr = FolderManagerRegisterNotificationProc(theNotifyUPP, NULL, 0);
- }
- else
- {
- theErr = noErr;
- theNotifyUPP = NULL;
- }
-
- theStatus = ICStart(&gIConfig, kOpenViaMapExtensionCode);
-
- if (theStatus == noErr)
- {
- theStatus = ICFindConfigFile(gIConfig, 0, NULL);
-
- if (theStatus == noErr)
- MainEventLoop();
-
- theStatus = ICStop(gIConfig);
- }
-
- if (theNotifyUPP)
- {
- theErr = FolderManagerUnregisterNotificationProc(theNotifyUPP, 0);
- DisposeFolderManagerNotificationUPP(theNotifyUPP);
- }
-
- theErr = AERemoveEventHandler(kAEFinderSuite, kAEInterceptOpen, theHandlerUPP, true);
- DisposeAEEventHandlerUPP(theHandlerUPP);
- }
- }